package com.lcf.framework.vo;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分页返回对象
 *
 * @author lcf
 */
@Data
public class PageVO<VO> {
    @Schema(description = "总条数")
    private Long total;
    @Schema(description = "总页数")
    private Long pages;
    @Schema(description = "数据")
    private List<VO> list;

    public <DO> PageVO(Page<DO> page, Function<DO, VO> function) {
        this.total = page.getTotal();
        this.pages = page.getPages();

        this.list = CollectionUtils.isEmpty(page.getRecords()) ? Collections.emptyList() : page.getRecords().stream().map(function).collect(Collectors.toList());
    }

    public <DO> PageVO(Page<DO> page, Class<VO> clazz) {
        this(page, x -> BeanUtil.copyProperties(x, clazz));
    }

    public PageVO() {
        this.total = 0L;
        this.pages = 0L;

        this.list = Collections.emptyList();
    }
}
